package cn.copera.dyy.common.utils.filter.builder;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BuildSQLFilter {
	private static final String STRING = "java.lang.String";
	private static final String BigDecimal = "java.math.BigDecimal";
	private static final String INTEGER = "java.lang.Integer";
	private static final String FLOAT = "java.lang.Float";
	private static final String INT = "int";
	private static final String LONG = "java.lang.Long";
	private static final String MINLONG = "long";
    public static boolean judgeFiled(String filedType,String filedValue){
    	if(filedValue != null && !filedValue.toString().equals("")){
    		if(filedType.equals(BuildSQLFilter.STRING)){
    			return true;
    		}else if(filedType.equals(BuildSQLFilter.INT)||filedType.equals(BuildSQLFilter.INTEGER)||filedType.equals(BuildSQLFilter.LONG)||filedType.equals(BuildSQLFilter.MINLONG)||filedType.equals(BuildSQLFilter.FLOAT)||filedType.equals(BuildSQLFilter.BigDecimal)){
    			if(!filedValue.toString().equals("0") && !filedValue.toString()
						.equals("-1")){
		          return true;
	            }
    		}
    	}
    	
    	return false;
    }
	
	

	@SuppressWarnings("rawtypes")
	public static SQLParamter genernateQuerySql(Object obj,String condition,
			Map<String, String> fieldMap,
			Map<String, DateRange> dateMap,String type) {
		String where = "";
		String classname = "";
		List<SimpleField> params = new ArrayList<SimpleField>();
		BuildFilterBean filter = new BuildFilterBean();
		if (obj != null) {
			Class newClass = obj.getClass();
			classname = newClass.getSimpleName();
			Field[] fields = newClass.getDeclaredFields();
			AccessibleObject.setAccessible(fields, true);
			 
			for (Field f : fields) {
				

				try {
					Object o = f.get(obj);
					if (f.getType().getName().equals(BuildSQLFilter.STRING)) {
						boolean flag = judgeFiled(BuildSQLFilter.STRING, o==null?"":o.toString());
						if(flag&&!f.getName().equals("serialVersionUID")&&!f.getName().equals("id")){
							SimpleField field = new SimpleField();
							field.setName(f.getName());
							field.setValue(o.toString());
							field.setRelational(type);	
							field.setDataType(BuildFilterBean.CHAR);
							field.setParameter(f.getName());
							filter.add(field);
							params.add(field);
						}
					}else if(f.getType().getName().equals(BuildSQLFilter.INT)||f.getType().getName().equals(BuildSQLFilter.MINLONG)||f.getType().getName().equals(BuildSQLFilter.LONG)||f.getType().getName().equals(BuildSQLFilter.INTEGER)||f.getType().getName().equals(BuildSQLFilter.FLOAT)||f.getType().getName().equals(BuildSQLFilter.BigDecimal)){
						boolean flag = judgeFiled(f.getType().getName(), o==null?"":o.toString());
						if(flag&&!f.getName().equals("serialVersionUID")&&!f.getName().equals("id")){
							SimpleField field = new SimpleField();
							field.setName(f.getName());
							field.setValue(o.toString());
							field.setRelational(BuildFilterBean.EQ);
							field.setDataType(BuildFilterBean.NUMBER);
							field.setParameter(f.getName());
							filter.add(field);
							params.add(field);
						}
					}
					

				} catch (Exception e) {
					e.printStackTrace();

				}

			}
		}
		int i=0;
		if (fieldMap != null && fieldMap.size() > 0) {
			for (String fieldName : fieldMap.keySet()) {
				String value = fieldMap.get(fieldName);
				if (value != null && !value.equals("")
						&& !value.equals("-1")) {
					SimpleField field = new SimpleField();
					field.setName(fieldName);
					field.setValue(value);
					field.setRelational(BuildFilterBean.LK);
					field.setDataType(BuildFilterBean.CHAR);
					field.setParameter("p"+i);
					field.setType("object");
                    filter.add(field);
                    params.add(field);
                    i++;
				}
			}

		}
		if (dateMap != null && dateMap.size() > 0) {
			for (String  dateField : dateMap.keySet()) {
				DateRange dg = dateMap.get(dateField);
				if (dg.getStartDate() != null && dg.getEndDate() != null) {
					DateareaField publishdate = new DateareaField();
					publishdate.setName(dateField);
					publishdate.setStart(dg.getStartDate());
					publishdate.setEnd(dg.getEndDate());
					publishdate.setLogical(BuildFilterBean.AND);
					filter.add(publishdate);
				}
			}

		}
		if (filter.getFilter() != null && !filter.getFilter().equals("")) {
			where = " where " + filter.getFilter();
			
		}
		
		
		String QL = " FROM " + classname +  where;
		;
	
		return new SQLParamter(QL, params);

	}
	
	
	
	@SuppressWarnings("rawtypes")
	public static String genernateFilter(Object obj,
			Map<String, String> fieldMap,
			Map<String, DateRange> dateMap,String type) {
		
		
		BuildFilterBean filter = new BuildFilterBean();
		if (obj != null) {
			Class newClass = obj.getClass();
			
			Field[] fields = newClass.getDeclaredFields();
			AccessibleObject.setAccessible(fields, true);

			for (Field f : fields) {
				

				try {
					Object o = f.get(obj);
					if (f.getType().getName().equals(BuildSQLFilter.STRING)) {
						boolean flag = judgeFiled(BuildSQLFilter.STRING, o==null?"":o.toString());
						if(flag){
							SimpleField field = new SimpleField();
							field.setName(f.getName());
							field.setValue(o.toString());
							field.setRelational(type);	
							field.setDataType(BuildFilterBean.CHAR);
							field.setParameter(f.getName());
							filter.add(field);
						}
					}else if(f.getType().getName().equals(BuildSQLFilter.INT)||f.getType().getName().equals(BuildSQLFilter.MINLONG)||f.getType().getName().equals(BuildSQLFilter.LONG)||f.getType().getName().equals(BuildSQLFilter.INTEGER)||f.getType().getName().equals(BuildSQLFilter.FLOAT)||f.getType().getName().equals(BuildSQLFilter.BigDecimal)){
						boolean flag = judgeFiled(f.getType().getName(), o==null?"":o.toString());
						if(flag){
							SimpleField field = new SimpleField();
							field.setName(f.getName());
							field.setValue(o.toString());
							field.setRelational(BuildFilterBean.EQ);
							field.setDataType(BuildFilterBean.NUMBER);
							field.setParameter(f.getName());
							filter.add(field);
						}
					}
					

				} catch (Exception e) {
					e.printStackTrace();

				}

			}
		}
		if (fieldMap != null && fieldMap.size() > 0) {
			for (String fieldName : fieldMap.keySet()) {
				String value = fieldMap.get(fieldName);
				if (value != null && !value.equals("")
						&& !value.equals("-1")) {
					SimpleField field = new SimpleField();
					field.setName(fieldName);
					field.setValue(value);
					field.setRelational(BuildFilterBean.LK);
					field.setDataType(BuildFilterBean.CHAR);
                    filter.add(field);
				}
			}

		}
		if (dateMap != null && dateMap.size() > 0) {
			for (String  dateField : dateMap.keySet()) {
				DateRange dg = dateMap.get(dateField);
				if (dg.getStartDate() != null && dg.getEndDate() != null) {
					DateareaField publishdate = new DateareaField();
					publishdate.setName(dateField);
					publishdate.setStart(dg.getStartDate());
					publishdate.setEnd(dg.getEndDate());
					publishdate.setLogical(BuildFilterBean.AND);
					filter.add(publishdate);
				}
			}

		}
		
		
		return filter.getFilter() ;

	}
	public static void main(String[] args)throws Exception{
		
		
	}

}
